package com.itany.nmms.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.DictConstant;
import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.ProductTypeMapper;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.entity.ProductTypeExample;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper typeMapper;


    @Override
    public void addType(String name) throws RequestParameterErrorException, ProductTypeExistException {
//        非空校验
        if (ParameterUtil.isNull(name)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        // 商品类型名称不能重复
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andNameEqualTo(name);
        List<ProductType> types = typeMapper.selectByExample(example);
        if (!types.isEmpty()) {
            throw new ProductTypeExistException("该商品类型已经存在");
        }
        // 执行添加操作
        ProductType type = new ProductType();
        type.setName(name);
        // 状态默认启用
        type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        typeMapper.insertSelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<ProductType> findPage(String pageNo, String pageSize) {
        // 设置默认页码
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.PRODUCT_TYPE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.PRODUCT_TYPE_DEFAULT_PAGE_SIZE;
        }
        // 通过MyBatis分页插件PageHelper实现分页功能
        // 该插件实现分页共分为三步
        // 1.设置分页信息
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        // 2.获取业务数据,即:需要分页的数据
        List<ProductType> types = typeMapper.selectByExample(new ProductTypeExample());
        // 3.封装分页对象
        PageInfo<ProductType> typePage = new PageInfo<>(types);
        return typePage;
    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        ProductType type = new ProductType();
        type.setId(Integer.parseInt(id));
        type.setStatus(Integer.parseInt(status));
        if (type.getStatus() == StatusConstant.PRODUCT_TYPE_STATUS_ENABLED) {
            type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_DISABLED);
        } else {
            type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        }
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public void modifyType(String id, String name) throws RequestParameterErrorException, ProductTypeExistException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(name)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        // 商品类型名称不能重复
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andNameEqualTo(name)
                .andIdNotEqualTo(Integer.parseInt(id));
        List<ProductType> types = typeMapper.selectByExample(example);
        if (!types.isEmpty()) {
            throw new ProductTypeExistException("该商品类型已经存在");
        }
        ProductType type = new ProductType();
        type.setId(Integer.parseInt(id));
        type.setName(name);
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public ProductType findById(String id) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        return typeMapper.selectByPrimaryKey(Integer.parseInt(id));
    }
//查询商品类型启用的
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<ProductType> findEnabled() {
        ProductTypeExample example=new ProductTypeExample();
        example.or()
                .andStatusEqualTo(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        return typeMapper.selectByExample(example);
    }
//查询所有商品
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<ProductType> findAll() {
    ProductTypeExample example=new ProductTypeExample();
        return typeMapper.selectByExample(example);
    }

}
